package com.siashan.toolkit.lock4j;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.data.redis.RedisAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.StringRedisTemplate;

/**
 * RedisTemplate锁自动配置器
 *
 * @author siashan
 * @since v1.0.1
 */
@Configuration
@ConditionalOnClass(RedisOperations.class)
@AutoConfigureAfter({RedisAutoConfiguration.class,Lock4jAutoConfiguration.class})
public class RedisTemplateLockAutoConfiguration {

    @Autowired
    private LockTemplate lockTemplate;

    @Bean
    @ConditionalOnMissingBean
    public RedisTemplateLockExecutor redisTemplateLockExecutor(StringRedisTemplate stringRedisTemplate) {
        RedisTemplateLockExecutor redisTemplateLockExecutor = new RedisTemplateLockExecutor(stringRedisTemplate);
        lockTemplate.setLockExecutor(redisTemplateLockExecutor);
        return redisTemplateLockExecutor;
    }
}